/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deadlocksim.gui;
import javax.swing.*;
import deadlocksim.*;
import deadlocksim.backend.process.*;
import java.awt.Color;
import java.awt.event.*;
/**
 *
 * @author CCSCHMID
 */
public class ProcessButton extends GradientButton 
{
    //original declarations
    private DeadlockSim mySim;
    DeadlockProcess p;
    //public Color DEFAULT_COLOR;
    //start popupmenu stuff
    private JPopupMenu processPopupMenu;
    private JMenuItem freeProcessMenuItem;
    private JMenuItem deleteProcessMenuItem;
    private JMenuItem killProcessMenuItem;
    private JMenuItem cancelProcessMenuItem;
    private ActionListener processActionListener;
    private MouseListener processPopupListener;
     //end popupmenu stuff
    
    public ProcessButton(DeadlockProcess proc, DeadlockSim sim)
    {
        super();
        //original calls
        //DEFAULT_COLOR = this.getBackground();
        mySim = sim;
        p = proc;
        setText(getDisplayText());
        
        //start popupmenu calls
        initProcessPopupMenu();
        this.addMouseListener(processPopupListener);
        //end popupmenu calls
    }
    
    public boolean equals(Object o)
    {
        if(o instanceof ProcessButton)
        {
            ProcessButton otherB = (ProcessButton)o;
            if(otherB.p.getID() == this.p.getID())
                return true;
            else
                return false;
        }
        return false;
    }
    public String getDisplayText()
    {
        String baseText = "P" + p.getID();
        if(p.isDeadlocked())
            baseText = baseText + "  (DEADLOCKED)";
        else if(p.isHalted())
            baseText = baseText + "  (Halted)";
        else if(mySim.isRunning())
            baseText = baseText + "  (Running)";
        
        return baseText;
    }
    //original methods
    public DeadlockProcess getProcess()
    {
        return p;
    }
    public void refreshDisplay()
    {
        if(p.isDeadlocked())
        {
            
            //System.out.println("P id: " + p.getID() + " setBackground DEADLOCK");
            this.setColors(GuiConfig.PURPLE_MED, 
                    GuiConfig.PURPLE_DARK, 
                    GuiConfig.PURPLE_LIGHT);
            //this.setBackground(DEADLOCK_COLOR);
        }
        else if(p.isHalted())
        {
            this.setColors(GuiConfig.RED_MED, 
                    GuiConfig.RED_DARK, 
                    GuiConfig.RED_LIGHT);
        }
        else if(mySim.isRunning())
        {
            
            this.setColors(GuiConfig.GREEN_MED, 
                    GuiConfig.GREEN_DARK, 
                    GuiConfig.GREEN_LIGHT);
        }
        else
        {
            //System.out.println("P id: " + p.getID() + " setBackground DEFAULT");
            this.setColors(GuiConfig.BUT_COLOR_1, GuiConfig.BUT_COLOR_2, GuiConfig.BUT_COLOR_3);
            //this.setBackground(DEFAULT_COLOR);
        }
        setText(getDisplayText());
    }
    
    //start popupmenu methods
    public void initProcessPopupMenu()
    {
        processPopupMenu = new JPopupMenu("Process Menu");
                
        freeProcessMenuItem = new JMenuItem("Free ALL resources from process P" + 
                p.getID());
        freeProcessMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //perform free process operation
                mySim.getResourceManager().takeAllFromProcess(p);
                mySim.refreshDisplay();
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        processPopupMenu.add(freeProcessMenuItem);
        
        killProcessMenuItem = new JMenuItem("KILL process P" +
                p.getID());
        killProcessMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //perform kill process operation
                
                mySim.sendMessage("Killing process P" + p.getID() +"!", false);
                p.halt();
                mySim.refreshDisplay();
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        processPopupMenu.add(killProcessMenuItem);
        
        final String actionText = "DELETE process P" +
                p.getID() + " from simulator!";
        deleteProcessMenuItem = new JMenuItem(actionText);
        deleteProcessMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                int response = JOptionPane.showConfirmDialog(deleteProcessMenuItem, 
                        "Are you sure you want to " + actionText + "?");
                if(response == JOptionPane.OK_OPTION)
                { 
                    //perform delete process operation
                    mySim.destroyProcess(p);
                }
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        processPopupMenu.add(deleteProcessMenuItem);
        
        cancelProcessMenuItem = new JMenuItem("Cancel");
        cancelProcessMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //cancel the menu, nothing to do
                //System.out.println("Cancel");
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        processPopupMenu.add(cancelProcessMenuItem);
        
        processPopupListener = new PopupListener(processPopupMenu);
    }
    
    class PopupListener extends MouseAdapter {
        JPopupMenu popup;
        
        PopupListener(JPopupMenu popupMenu) {
            popup = popupMenu;
        }
        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }
        private void maybeShowPopup(MouseEvent e) {
            if (!e.isPopupTrigger()) {
                popup.show(e.getComponent(), e.getX(), e.getY());
                if(mySim.isRunning())
                    mySim.stopClock();
            }
        }
    }
    //end popupmenu methods
}
